iT邦幫忙

2023 iThome 鐵人賽

DAY 22
0
Mobile Development

Android與Spring Boot開發學習之旅系列 第 22

開發學習之旅 Day22 - 記帳應用API:收入資料請求

  • 分享至 

  • xImage
  •  

建立Entity類

定義實體類,該類包含資產的相關資訊,用於映射到assets資料庫表。

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "_assets")
public class AssetsEntity {
    @CreatedBy
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private AppUserEntity appUser;
    @Id
    @GeneratedValue
    private Long id;
    private String assetsType;
    private String assetsName;
    private Integer assetsValue;
    @LastModifiedDate
    @JsonIgnore
    private LocalDate recorded_at;
}

建立Repository類

Assets Repository 接口,通過自定義的JPQL查詢對AssetsEntity進行資料庫操作。
用於查詢資料庫的自訂SQL查詢語句,根據日期和使用者篩選 AssetsEntity 物件。

@Repository
public interface AssetsRepository extends JpaRepository<AssetsEntity, Long> {
    @Query("SELECT e FROM AssetsEntity e WHERE DATE(e.recorded_at) = :date AND e.appUser = :appUser")
    List<AssetsEntity> findAssetsByDateAndAppUser(@Param("date") LocalDate date, AppUserEntity appUser);

}

建立Service類

用於處理資產相關邏輯,返回相應的響應數據。

@Service
@AllArgsConstructor
public class AssetsService {

    private final AssetsRepository assetsRepository;

    public Map<String, Object> getAssets(String request) {
        var response = new LinkedHashMap<String, Object>();
        var appUser = (AppUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        var Data = new ArrayList<>();
        LocalDate date = LocalDate.parse(request, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        var assetsData = assetsRepository.findAssetsByDateAndAppUser(date,appUser);
        if (!assetsData.isEmpty()){
            for(var assets:assetsData){
                Data.add(assets);
            }
        }
        response.put("status", "成功");
        response.put("message","ok");
        response.put("assetsDataList", Data);
        return  response;
    }

    public Map<String, Object> addAssets(AssetsRequest request){
        var response = new LinkedHashMap<String, Object>();
        var appUser = (AppUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        var assetsData = AssetsEntity
                .builder()
                .appUser(appUser)
                .assetsType(request.getAssetsType())
                .assetsName(request.getAssetsName())
                .assetsValue(request.getAssetsValue())
                .recorded_at(request.getRecorded_at())
                .build();
        assetsRepository.save(assetsData);
        response.put("status", "成功");
        response.put("message", "ok");
        return response;
    }

    public Map<String, Object> deleteAssets(Long request){
        var response = new LinkedHashMap<String, Object>();
        assetsRepository.deleteById(request);
        response.put("status", "成功");
        response.put("message", "ok");
        return response;
    }

}

建立Controller類

Spring Boot控制器,用於處理支出相關的 HTTP 請求。

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user")
@Tag(name = "資產模塊")
public class AssetsController {

    private final AssetsService service;

    @GetMapping("/assets")
    @Operation(summary = "資產列表資料")
    public ResponseEntity<?> getAssets(
            @RequestParam("date") String date
    ) {
        return ResponseEntity.ok(service.getAssets(date));
    }

    @PostMapping("/assets")
    @Operation(summary = "新增資產資料")
    public ResponseEntity<?> addAssets(
            @RequestBody AssetsRequest request
    ) {
        return ResponseEntity.ok(service.addAssets(request));
    }

    @DeleteMapping("/assets")
    @Operation(summary = "刪除資產記錄")
    public ResponseEntity<?> deleteAssets(
            @RequestParam("id") Long request
    ) {
        return ResponseEntity.ok(service.deleteAssets(request));
    }
}

建立Request類

@Data
@Schema(name = "AssetsRequest", description = "資產新增請求")
public class AssetsRequest {
    @Schema(description = "資產名稱")
    private String assetsName;
    @Schema(description = "資產金額")
    private Integer assetsValue;
    @Schema(description = "資產類型")
    private String assetsType;
    @Schema(description = "使用者記錄時間")
    private LocalDate recorded_at;
}

上一篇
開發學習之旅 Day21 - 記帳應用:Android端請求支出資料
下一篇
開發學習之旅 Day23 - 記帳應用:Android端請求資產資料
系列文
Android與Spring Boot開發學習之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言